package com.salesmanager.web.admin.controller;

import com.salesmanager.web.admin.entity.web.Menu;
import com.salesmanager.web.utils.CustomTimestampEditor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Liutong on 9/2/15.
 */
public abstract class AbstractController {

    private static final Logger LOGGER = LoggerFactory.getLogger(AbstractController.class);

    protected <T> T getSessionAttribute(final String key, HttpServletRequest request) {
        return (T) com.salesmanager.web.utils.SessionUtil.getSessionAttribute(key, request);

    }

    protected void setSessionAttribute(final String key, final Object value, HttpServletRequest request) {
        com.salesmanager.web.utils.SessionUtil.setSessionAttribute(key, value, request);
    }


    protected void removeAttribute(final String key, HttpServletRequest request) {
        com.salesmanager.web.utils.SessionUtil.removeSessionAttribute(key, request);
    }

    protected void setMenu(Model model,HttpServletRequest request,List<String> activeMenuList,String currMenu){
        Map<String,String> activeMenus = new HashMap<String,String>();

        for(String menu : activeMenuList){
            activeMenus.put(menu, menu);
        }

        @SuppressWarnings("unchecked")
        Map<String, Menu> menus = (Map<String, Menu>)request.getAttribute("MENUMAP");

        Menu currentMenu = (Menu)menus.get(currMenu);
        model.addAttribute("currentMenu",currentMenu);
        model.addAttribute("activeMenus",activeMenus);
    }


    @ExceptionHandler(Exception.class)
    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
    public ModelAndView handleException(Exception ex) {

        ModelAndView model = null;
        if (ex instanceof AccessDeniedException) {
            model = new ModelAndView("error/access_denied");
        } else {
            model = new ModelAndView("error/generic_error");
            model.addObject("stackError", ex);
            model.addObject("errMsg", ex.getMessage());
        }
        LOGGER.error(ex.getMessage(),ex);

        return model;

    }

    @InitBinder
    protected void initBinder(
            WebDataBinder binder) throws ServletException {

        SimpleDateFormat datetimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        datetimeFormat.setLenient(false);

        binder.registerCustomEditor(java.util.Date.class, new CustomDateEditor(datetimeFormat, true));
        binder.registerCustomEditor(java.sql.Timestamp.class, new CustomTimestampEditor(datetimeFormat, true));
    }

}